public class isPalindrome {
    /**
     * 回文链表判断
     */
    public class ListNode{
        int val;
        ListNode next;
        ListNode(){}
        ListNode(int val){
            this.val=val;
        }
        ListNode(int val, ListNode next){
            this.val=val;
            this.next=next;
        }
    }

    public boolean method(ListNode head){
        if(head == null || head.next ==null){
            return true;
        }
        ListNode slow = head;
        ListNode fast = head;
        ListNode pre = head;
        ListNode prepre = null;
        while(fast!=null && fast.next!=null){
            pre=slow;
            slow=slow.next;
            fast=fast.next.next;
            pre.next=prepre;
            prepre=pre;
        }
        if(fast!=null){
            slow=slow.next;
        }
        while(pre!=null && slow!=null){
            if(pre.val!= slow.val){
                return false;
            }
            pre=pre.next;
            slow=slow.next;
        }
        return true;

    }
    public static void main(String[] args) {
        isPalindrome solution = new isPalindrome();

        // 创建示例回文链表
        ListNode head = solution.new ListNode(1);
        head.next = solution.new ListNode(2);
        head.next.next = solution.new ListNode(3);
        head.next.next.next = solution.new ListNode(2);
        head.next.next.next.next = solution.new ListNode(1);

        // 调用方法判断回文链表
        boolean result = solution.method(head);

        // 输出结果
        System.out.println(result); // 输出：true，表示链表是回文的
    }
}
